//app.js
require('zhuge-wx.min.js')
App.zhuge.load('25d3d0bd28924aaaab43dcef14798b74');
let api = require('api/index.js')
let imgUlr = 'http://www.didisport.cn/public'
App({
  onLaunch: function () {
    // 展示本地存储能力
    var token = wx.getStorageSync('token');
    
    //this.locationCity() // 获取位置信息
    // 获取用户信息
    this.getLocation();
    this.Logins();

    
  },
  Logins:function(){
    wx.login({
      success: function (res) {
        let _this = this;
        if (res.code) {
          //发起网络请求
          api.login(res.code, (res) => {
            console.log(res)
            wx.setStorageSync('session_key', {
              "session_key": res.data.result.session_key,
            });
            wx.setStorageSync('openid', {
              "openid": res.data.result.openid
            })
          })

        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });
  },
  globalData: {
    userInfo: null,
    session_key:'',
    Refresh:false,
    indexFormOne: { // 首页表单数据
      address: '',
      age: '',
      sex: '',
      purpose: '',
      money: '',
      circle: '',
      phone: ''
    },
    indexFormTwo: {
      address: '',
      age: '',
      sex: '',
      purpose: '',
      money: '',
      circle: '',
      phone: ''
    },
    indexFormThree: {
      address: '',
      age: '',
      sex: '',
      purpose: '',
      money: '',
      circle: '',
      phone: ''
    } ,
    vennueQuery: {}, // 从评测跳到找场馆传递数据
    IndexQuery: {}, // 以及项目类别跳到找场馆传递数据
    serchData: "", // 搜索框数据
    bufferData: [], // 缓存搜索数据
    currentCity: {region_name: '北京市', region_id:""}, // 当前城市
    locationCity: { region_name: '北京市', region_id: "" } // 定位 城市
  },
  loginCallback: function(callback){
    // 登录
    wx.login({
      success: res => {
        api.login(res.code, (res) => {
          console.log(res)
          let openId = res.data.result;
          callback(openId)
        })
      }
    })
  },
  
  getLocation:function(){
    let that = this;
    var  data;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) { //授权
        console.log(res)
        wx.setStorageSync('key',{
          latitude: res.latitude,
          longitude: res.longitude
        })   
        var latitude = res.latitude
        var longitude = res.longitude
      },
      fail:function(res){ //不授权
        wx.setStorageSync('key', {
          latitude: 39.9088600000,
          longitude: 116.3973900000
          
        }) 
      }
    })
  },
  locationCity: function () {
    let _this = this;
    return new Promise(function (resolve, reject) {
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          if (true) {
            var latitude = res.latitude
            var longitude = res.longitude
            api.locationPosition(latitude, longitude, function (city) {
              _this.cityJudje(city) //判断城市是否已经开通
              resolve(city)
            })
          }
        },
        fail: function () {
          let cityObj = { region_name: '北京市', region_id: "" };
          _this.globalData.locationCity = cityObj;
          _this.globalData.currentCity = cityObj;
        }
      })
    })
  },
  cityJudje: function (cityname) {
    let _this = this;
    

    api.indexCity((cityList) => {  // 请求开通城市列表
      let cityArr = cityList.data.result;
      // 判断定位城市是否已经开通
      let cityJudge = cityArr.some(function (city) {
        return city.region_name == cityname;
      })

      if (cityJudge) { // 已开通城市
        cityArr.forEach(function (item) { // 循环遍历已开通城市里是否有定位城市 ，如果有  就当前城市与定位城市  都设置为当前城市对象 
          if (item.region_name == cityname) {
            _this.globalData.locationCity = item;
            _this.globalData.currentCity = item;
          }
        })

      } else {  // 未开通城市  如果当前城市未开通 设置定位城市为未开通  当前城市为北京
        // _this.globalData.currentCity = cityArr[0];
        _this.globalData.locationCity = {
          addr_id: 888888,
          addr_name: cityname + '(未开通)'
        }
      }

    })
  },
  messages:function(){
    let token = wx.getStorageSync("token").token
    if (!token) {
      console.log("我没有token") 
      return 0
    }else{
      return 1
    }
   },
  accredit:function(e){
      
  }
})